/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.FolhaPagamento;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.Funcionario;
import entidade.persistencia.EntradaSaida;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "folhaPagamento")
public class FolhaPagamentoConverter {
    private FolhaPagamento entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of FolhaPagamentoConverter */
    public FolhaPagamentoConverter() {
        entity = new FolhaPagamento();
    }

    /**
     * Creates a new instance of FolhaPagamentoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public FolhaPagamentoConverter(FolhaPagamento entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getFolhaPagamentoId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getEntradaSaidaCollection();
        getFuncionarioId();
    }

    /**
     * Creates a new instance of FolhaPagamentoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public FolhaPagamentoConverter(FolhaPagamento entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for folhaPagamentoId.
     *
     * @return value for folhaPagamentoId
     */
    @XmlElement
    public Integer getFolhaPagamentoId() {
        return (expandLevel > 0) ? entity.getFolhaPagamentoId() : null;
    }

    /**
     * Setter for folhaPagamentoId.
     *
     * @param value the value to set
     */
    public void setFolhaPagamentoId(Integer value) {
        entity.setFolhaPagamentoId(value);
    }

    /**
     * Getter for dataPagamento.
     *
     * @return value for dataPagamento
     */
    @XmlElement
    public Date getDataPagamento() {
        return (expandLevel > 0) ? entity.getDataPagamento() : null;
    }

    /**
     * Setter for dataPagamento.
     *
     * @param value the value to set
     */
    public void setDataPagamento(Date value) {
        entity.setDataPagamento(value);
    }

    /**
     * Getter for salario.
     *
     * @return value for salario
     */
    @XmlElement
    public Float getSalario() {
        return (expandLevel > 0) ? entity.getSalario() : null;
    }

    /**
     * Setter for salario.
     *
     * @param value the value to set
     */
    public void setSalario(Float value) {
        entity.setSalario(value);
    }

    /**
     * Getter for adiantamento.
     *
     * @return value for adiantamento
     */
    @XmlElement
    public Float getAdiantamento() {
        return (expandLevel > 0) ? entity.getAdiantamento() : null;
    }

    /**
     * Setter for adiantamento.
     *
     * @param value the value to set
     */
    public void setAdiantamento(Float value) {
        entity.setAdiantamento(value);
    }

    /**
     * Getter for extra.
     *
     * @return value for extra
     */
    @XmlElement
    public Float getExtra() {
        return (expandLevel > 0) ? entity.getExtra() : null;
    }

    /**
     * Setter for extra.
     *
     * @param value the value to set
     */
    public void setExtra(Float value) {
        entity.setExtra(value);
    }

    /**
     * Getter for entradaSaidaCollection.
     *
     * @return value for entradaSaidaCollection
     */
    @XmlElement
    public EntradaSaidasConverter getEntradaSaidaCollection() {
        if (expandLevel > 0) {
            if (entity.getEntradaSaidaCollection() != null) {
                return new EntradaSaidasConverter(entity.getEntradaSaidaCollection(), uri.resolve("entradaSaidaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for entradaSaidaCollection.
     *
     * @param value the value to set
     */
    public void setEntradaSaidaCollection(EntradaSaidasConverter value) {
        entity.setEntradaSaidaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for funcionarioId.
     *
     * @return value for funcionarioId
     */
    @XmlElement
    public FuncionarioConverter getFuncionarioId() {
        if (expandLevel > 0) {
            if (entity.getFuncionarioId() != null) {
                return new FuncionarioConverter(entity.getFuncionarioId(), uri.resolve("funcionarioId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for funcionarioId.
     *
     * @param value the value to set
     */
    public void setFuncionarioId(FuncionarioConverter value) {
        entity.setFuncionarioId((value != null) ? value.getEntity() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the FolhaPagamento entity.
     *
     * @return an entity
     */
    @XmlTransient
    public FolhaPagamento getEntity() {
        if (entity.getFolhaPagamentoId() == null) {
            FolhaPagamentoConverter converter = UriResolver.getInstance().resolve(FolhaPagamentoConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved FolhaPagamento entity.
     *
     * @return an resolved entity
     */
    public FolhaPagamento resolveEntity(EntityManager em) {
        List<EntradaSaida> entradaSaidaCollection = entity.getEntradaSaidaCollection();
        List<EntradaSaida> newentradaSaidaCollection = new java.util.ArrayList<EntradaSaida>();
        for (EntradaSaida item : entradaSaidaCollection) {
            newentradaSaidaCollection.add(em.getReference(EntradaSaida.class, item.getEntradaSaidaId()));
        }
        entity.setEntradaSaidaCollection(newentradaSaidaCollection);
        Funcionario funcionarioId = entity.getFuncionarioId();
        if (funcionarioId != null) {
            entity.setFuncionarioId(em.getReference(Funcionario.class, funcionarioId.getFuncionarioId()));
        }
        return entity;
    }
}
